import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 使用本地文件路径
file_path = "D:\wenjian\getee qier\penguins.csv"

# 读取 CSV 文件
df = pd.read_csv(file_path)

# 打印数据框的前几行，以验证数据是否正确读取
print(df.head(5))
# Let's visualize the distribution of the penguins species with a bar plot in matplotlib
species_counts = df['Species'].value_counts()

# 绘制柱状图
plt.bar(species_counts.index, species_counts.values)

# 添加标签和标题
plt.xlabel('Species')
plt.ylabel('Count')
plt.title('Distribution of Penguin Species')

# 显示图形
plt.show()

# Let's visualize with boxplots how the FlipperLength, CulmenLength and CulmenDepth are distributed for each species
# importing seaborn
import seaborn as sns


# Show rows with missing values
print(df[df.isnull().any(axis=1)])

# Drop rows with missing values
df = df.dropna()

# Let's prepare for training:
# 1. Split the data into features and labels
# 2. Split the data into training and test sets

# Split the data into features and labels
# features are CulmenLength, CulmenDepth, FlipperLength
# labels are Species
features = ['CulmenLength', 'CulmenDepth', 'FlipperLength']
labels = 'Species'
X, y = df[features].values, df[labels].values

# Split the data into training and test sets in a way to have 30% of the data for testing
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=0)

# 使用箱线图可视化 FlipperLength, CulmenLength 和 CulmenDepth 在每个物种中的分布
sns.boxplot(x='Species', y='FlipperLength', data=df)
sns.boxplot(x='Species', y='CulmenLength', data=df)
sns.boxplot(x='Species', y='CulmenDepth', data=df)

# 显示图形
plt.show()

# Let's train a Logistic Regression model
# 1. Create a multiclass Logistic Regression model
# 2. Train the model

# Create a multiclass Logistic Regression model
from sklearn.linear_model import LogisticRegression

model = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=1000)

# Train the model
model.fit(X_train, y_train)

# Let's evaluate the model
# 1. Predict the labels of the test set
# 2. Calculate the accuracy of the model
y_hat = model.predict(X_test)
acc = np.average(y_hat == y_test)
print('Accuracy:', acc)




